package com.googlecode.beanfiles.csv;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.beanfiles.TranslationException;

import au.com.bytecode.opencsv.CSVReader;

public class CSVReaderUtil {

    protected static Log log() {
        return LogFactory.getLog(CSVReaderUtil.class);
    }

//    public static Iterator<Collection<Object>> parseExcelCSV(Reader input) {
//        try {
//            return buildIterator(ExcelCSVParser.parse(input));
//        } catch (IOException e) {
//            log().error(e);
//            throw new TranslationException(e);
//        }
//    }

    @SuppressWarnings("unchecked")
    public static Iterator<Collection<Object>> parseStandardCSV(Reader input) {
        try {
        	return buildIterator((List<String[]>) new CSVReader(input).readAll());
        } catch (IOException e) {
            log().error(e);
            throw new TranslationException(e);
        }
    }

    private static Iterator<Collection<Object>> buildIterator(List<String[]> table) {
    	Collection<Collection<Object>> collection = new ArrayList<Collection<Object>>();
    	for (String[] line : table) {
    		collection.add(Arrays.asList((Object[]) line));
    	}
    	return collection.iterator();
    }

}
